/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.lidejia.hivetotalsort.type;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 *
 * @author lidejia
 */
public class HiveSortKey implements WritableComparable<HiveSortKey> {

    static {
        WritableComparator.define(HiveSortKey.class, new HiveSortKeyComparator());
    }
    private ArrayWritable keys = new ArrayWritable(HiveType.class);

    public HiveSortKey() {
    }

    public void setKeys(Writable[] writables) {
        keys.set(writables);
    }

    public void write(DataOutput d) throws IOException {
        keys.write(d);
    }

    public void readFields(DataInput di) throws IOException {
        keys.readFields(di);
    }

    public int compareTo(HiveSortKey o) {
        Writable[] self = this.keys.get();
        Writable[] that = o.keys.get();
        int cmp = 0;
        for (int i = 0; i < self.length; i++) {
            HiveType selfType = (HiveType) self[i];
            HiveType thatType = (HiveType) that[i];
            cmp = selfType.compareTo(thatType);
            if (cmp != 0) {
                break;
            }
        }
        return cmp;
    }
}
